import os
import json
from pathlib import Path

class ConfigManager:
    def __init__(self):
        self.config_dir = os.path.join(Path.home(), ".config", "gdradio")
        self.config_file = os.path.join(self.config_dir, "config.json")
        
        if not os.path.exists(self.config_dir):
            os.makedirs(self.config_dir)
            
        self._load_config()
    
    def _load_config(self):
        defaults = {
            "volume": 80,
            "last_station": None,
            "window_size": [800, 600],
            "theme": "default"
        }
        
        try:
            with open(self.config_file, 'r') as f:
                self.config = {**defaults, **json.load(f)}
        except (FileNotFoundError, json.JSONDecodeError):
            self.config = defaults
            self._save_config()
    
    def _save_config(self):
        with open(self.config_file, 'w') as f:
            json.dump(self.config, f, indent=4)
    
    def get(self, key):
        return self.config.get(key)
    
    def set(self, key, value):
        self.config[key] = value
        self._save_config()